<?php
/**
 * 素材维护任务
 * @author ice<iceinto@mallog.com.cn>
 */
class MaterialAction extends CommonAction
{

    public static $Grade = array();

    public function _initialize()
    {
        // 1展示底图 2小图底图 3添加品牌  4商场海报 5楼层中图 6商品特卖 7优惠领取
        self::$Grade['1'] = array(1,);
        self::$Grade['2'] = array(1, 2, 3, 4);
        self::$Grade['3'] = array(1, 2, 3, 4, 5, 6, 7);
        parent::_initialize();
        $this->assign('nav', 9);
        $this->assign('web_title', '维护任务');
    }

    /**
     * 查看当前等级
     * @return array
     * @author ice<iceinto@mallog.com.cn>
     */
    public function _checkGrade($uid)
    {
        //获取用户等级   role 3 level
        $level = D('User')->getMLevel($uid);
        $level = intval($level['level']);
        if ($level) {
            return self::$Grade[$level];
        } else {
            return false;
        }
    }

    /**
     * 维护任务首页
     * @author ice<iceinto@mallog.com.cn>
     */
    public function index()
    {
        $mModel = M('Maintenance');
        $allCount = $mModel->count();
        $okCount = $mModel->where('status = 100')->count();
        $waitAllotCount = $mModel->where('status = 1')->count();
        $cancelCount = $mModel->where('status = 99')->count();
        for ($i = 1; $i <= 7; $i++) {
            $map['status'] = 1;
            $map['type'] = $i;
            $this->assign('wait' . $i, intval($mModel->where($map)->count()));
        }
        $this->assign('waitCount', $allCount - $okCount - $cancelCount);
        $this->assign('waitAllotCount', $waitAllotCount);
        $this->assign('cancelCount', $cancelCount);
        $this->assign('allCount', $allCount);
        $this->display();
    }

    /**
     * 获取当前用户任务列表
     * @author ice<iceinto@mallog.com.cn>
     */
    public function task()
    {
        $uid = $this->getLoginUserInfo();
        $MaintenanceAllot = D('MaintenanceAllot');
        $waitCount = $MaintenanceAllot->getUserWaitCount($uid);
        $Page = $this->getPage($waitCount);
        $tasks = $MaintenanceAllot->getUserWaitTasks($uid, $Page->firstRow . ',' . $Page->listRows);
        $this->assign('tasks', $tasks);
        $this->assign('page', $Page->show());
        $this->display();
    }

    /**
     * 获取任务详细
     * @author ice<iceinto@mallog.com.cn>
     */
    public function detail()
    {
        $itemid = intval(getGet('id'));
        $maintenanceAllot = M('maintenanceAllot')->find($itemid);
        if ($maintenanceAllot['uid'] != $this->getLoginUserInfo()) {
            $this->error('任务不是你的你没有权利进行任何操作！');
        }
        if ($maintenanceAllot) {
            $maintenance = M('maintenance')->find($maintenanceAllot['mid']);
            if ($maintenance) {
                $maintenancePlan = M('maintenancePlan')->find($maintenance['pid']);
                if ($maintenancePlan) {
                    //设置信息
                    $this->assign('allot', $maintenanceAllot);
                    $this->assign('plan', $maintenancePlan);
                    $this->assign('task', $maintenance);
                    if ($this->isPost()) {
                        //
                        $note = hh(getPost('note'));
                        if (empty($note)) {
                            $this->error('麻烦你写下备注，方便以后查看！');
                            exit;
                        }
                        $this->autoCheckToken();
                        //修改完成时间
                        $data = array();
                        $data['itemid'] = $itemid;
                        $data['end_time'] = time();
                        $data['note'] = $note;
                        M('maintenanceAllot')->save($data);
                        unset($data);
                        $data['itemid'] = $maintenance['itemid'];
                        $data['status'] = 100;
                        M('maintenance')->save($data);
                        $this->assign('jumpUrl', U('task'));
                        $this->success('完成任务');
                        exit;
                    } else {
                        $this->display();
                    }
                } else {
                    $this->error('任务计划错误！');
                }
            } else {
                $this->error('任务信息错误！');
            }
        } else {
            $this->error('任务分配信息错误！');
        }
    }

    /**
     * 任务给别人
     */
    public function push()
    {

    }

    /**
     * 添加维护任务
     * @author ice<iceinto@mallog.com.cn>
     */
    public function add()
    {
        $MaintenancePlan = D('MaintenancePlan');
        $plans = $MaintenancePlan->getActivityList();
        //获取计划列表
        if ($this->isPost()) {
            $this->autoCheckToken();
            $data = $this->getTaskPostData();
            $data['uid'] = $this->getLoginUserInfo();
            $rs = M('Maintenance')->add($data);
            if ($rs) {
                $this->assign('successMessage', $data['brand'] . '任务添加成功！');
            } else {
                $this->assign('errorMessage', $data['brand'] . '任务添加失败！');
            }
        }
        $this->assign('plans', $plans);
        $this->display();
    }

    /**
     * 任务分配
     * @author ice<iceinto@mallog.com.cn>
     */
    public function pull()
    {
        //任务获取：规则为同一个类型任务维护 只能能有一个
        $uid = $this->getLoginUserInfo();
        $MaintenanceAllotModel = D('MaintenanceAllot');
        $waitCount = $MaintenanceAllotModel->getUserWaitCount($uid);
        if ($waitCount) {
            $this->error('不要贪心啊！你已经有一个任务啦快去完成在来！');
        } else {
            //获取上一个任务领取的时间如果 不到一分钟 则禁止领取
            $lastAllot = $MaintenanceAllotModel->getUserLastAllot($uid);
            if (!empty($lastAllot)) {
                if ($lastAllot['add_time'] > (time() - 60)) {
                    $this->error('神一般的速度啊！一分钟内一个牛，你可以休息会在来领取。');
                    exit;
                }
            }
            $MaintenanceModel = M('Maintenance');
            //执行一个任务分配
            //获取一个任务
            $map = array();
            $map['status'] = 1; //等待处理
            $grade = $this->_checkGrade($uid);
            if ($grade) {
                $map['type'] = array('in', implode(',', $grade));
            } else {
                $this->error('你没有权限领取任务!');
            }
            $task = $MaintenanceModel->field('itemid')->where($map)->order('priority DESC,itemid')->find();
            //添加分配数据
            if ($task) {
                $allot = array();
                $allot['uid'] = $uid;
                $allot['mid'] = $task['itemid'];
                $allot['add_time'] = time();
                $aid = $MaintenanceAllotModel->add($allot);
                if ($aid) {
                    //修改任务状态
                    $data = array();
                    $data['itemid'] = $task['itemid'];
                    $data['status'] = 2;
                    $rs = $MaintenanceModel->save($data);
                    if ($rs) {
                        //跳转到任务详细
                        redirect(U('/Task/Material/detail/id/' . $aid));
                    } else {
                        M('MaintenanceAllot')->delete($aid);
                    }
                }
            } else {
                $this->error('现在没有任务可以分配啦，注意看首页统计！');
            }
        }
        $this->display();
    }

    /**
     * 获取任务提交数据
     * @param bool $isEdit
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getTaskPostData($isEdit = false)
    {
        $data = array();
        $data['pid'] = intval(getPost('plan'));
        $data['type'] = intval(getPost('type'));
        $data['priority'] = intval(getPost('priority'));
        $data['end_count'] = intval(getPost('end_count'));
        $data['now_count'] = intval(getPost('now_count'));
        $data['end_time'] = strtotime(getPost('end_time'));
        $data['note'] = hh(getPost('note'));
        $data['brand'] = hh(getPost('brand'));
        if ($isEdit) {
        } else {
            $data['add_time'] = time();
            $data['status'] = 1;
            $data['user'] = $this->getLoginUserInfo();
        }
        if ($data['now_count'] >= $data['end_count']) {
            $this->error('开始数量必须小于结束后的数量！');
        }
        if ($data['end_time'] < $this->getThisDayTime()) {
            $this->error('结束时间必须超过当天时间！');
        }
        return $data;
    }

    /**
     * 修改任务情况
     * @author ice<iceinto@mallog.com.cn>
     */
    public function edit()
    {
        $itemid = intval(getGet('id'));
        $maintenance = M('maintenance')->find($itemid);
        if (!$maintenance) {
            $this->error('任务信息不正确！');
        }
        $map = array();
        $map['mid'] = $itemid;
        $maintenanceAllot = M('maintenanceAllot')->where($map)->find();
        $cancel = M('maintenanceCancel')->where($map)->count();
        if ($this->isPost()) {
            $status = intval(getPost('status'));
            $note = hh(getPost('note'));
            if ($status == 99) {
                //作废可以没有分配和分配的任务
                if ($maintenance['status'] == 1 || $maintenance['status'] == 2) {
                    $mcModel = M('maintenanceCancel');
                    unset($map);
                    $map['mid'] = $itemid;
                    if ($mcModel->where($map)->count()) {
                        $this->error('已经做过废啦不能重复作废！');
                    }
                    //执行作废
                    //1.修改任务状态
                    $data = array();
                    $data['itemid'] = $itemid;
                    $data['status'] = $status;
                    if (M('maintenance')->save($data)) {
                        //添加作废记录
                        unset($data);
                        $data['mid'] = $itemid;
                        $data['add_time'] = time();
                        $data['uid'] = $this->getLoginUserInfo();
                        $data['note'] = $note;
                        if ($mcModel->add($data)) {
                            $maintenance = M('maintenance')->find($itemid);
                            $this->successMessage('作废成功！');
                        }
                    } else {
                        $this->error('作废失败！');
                    }
                } else {
                    $this->error('当前状态不能作废！');
                }
            } else if ($status == 2) {
                unset($map);
                //获取废除信息
                $map['mid'] = $itemid;
                $cancel = M('maintenanceCancel')->where($map)->find();
                if (empty($cancel)) {
                    $this->error('作废信息不存在！');
                } else {
                    //判断当前是否有任务有任务不能进行废除恢复
                    if ($maintenance['status'] != 99) {
                        $this->error('当前状态不能进行作废恢复！');
                    }
                    //修改废除状态
                    $uid = $this->getLoginUserInfo();
                    $waitCount = D('MaintenanceAllot')->getUserWaitCount($uid);
                    if (!empty($waitCount)) {
                        $this->error('你当前还有任务，请你先做当前任务！');
                    }
                    if ($cancel['uid'] != $uid) {
                        $this->error('必须作废人才可以恢复！');
                    } else {
                        $data = array();
                        $data['itemid'] = $itemid;
                        $data['status'] = 2;
                        if (M('maintenance')->save($data)) {
                            $this->error('成功恢复！');
                        } else {
                            $this->error('恢复失败！');
                        }
                    }
                }
            }
        }
        unset($map);
        $map['itemid'] = $maintenance['pid'];
        $maintenancePlan = M('maintenancePlan')->find($maintenance['pid']);
        $this->assign('allot', $maintenanceAllot);
        $this->assign('plan', $maintenancePlan);
        $this->assign('task', $maintenance);
        $this->assign('cancel', $cancel);
        $this->display();
    }

    /**
     * 美工提示
     * @author Ice<iceinto@mallog.com.cn>
     */
    public function artist()
    {
        $status = 0;
        $data = 0;
        $uid = $this->getLoginUserInfo();
        $waitCount = D('MaintenanceAllot')->getUserWaitCount($uid);
        if ($waitCount) {
            $data = '快去完成当前维护任务';
            $status = 1;
        } else {
            $MaintenanceModel = M('Maintenance');
            $map = array();
            $map['status'] = 1; //等待处理
            $grade = $this->_checkGrade($uid);
            if ($grade) {
                $map['type'] = array('in', implode(',', $grade));
            } else {
                $this->error('你没有权限领取任务!');
            }
            $tCount = $MaintenanceModel->where('status=1')->count();
            if ($tCount) {
                $data = '有新的任务可以接受';
                $status = 1;
            }
        }
        $this->ajaxReturn($data, 0, $status);
    }

    /**
     * 对当前人员认为任务
     * @author Ice<iceinto@mallog.com.cn>
     */
    public function statistics()
    {
        //时间计算
        import("@.ORG.Util.Date");
        $Date = new Date();
        $year = intval(getGet('year'));
        $month = intval(getGet('month'));
        $grade = intval(getGet('grade'));
        if ($year && $month) {
            $Date = new Date($year . '-' . $month . '-1');
            $this->assign('month', $month);
        } else {
            $this->assign('month', date('n'));
        }
        $fireTime = $Date->firstDayOfMonth()->format("%Y-%m-%d");
        $lastTime = $Date->lastDayOfMonth()->format("%Y-%m-%d");

        $fireTime = strtotime($fireTime);
        $lastTime = strtotime($lastTime);
        //获取登陆用户基本信息
        $uid = $this->getLoginUserInfo();
        $grades = $this->_checkGrade($uid);
        //根据条件获取任务列表
        $grade = empty($grade) ? 1 : $grade;
        $map = array();
        $map['m_maintenance_allot.uid'] = $uid;
        $map['m_maintenance.type'] = $grade;
        //写入结束时间
        $map['m_maintenance.end_time'] = array(array('egt', $fireTime), array('elt', $lastTime));
        $MaintenanceAllot = D('MaintenanceAllot');
        $taskCount = $MaintenanceAllot->getTaskCount($map);
        $taskImageCount = $MaintenanceAllot->getTaskImageCount($map);
        $Page = $this->getPage($taskCount);
        $tasks = $MaintenanceAllot->getTasks($map, $Page->firstRow . ',' . $Page->listRows);
        $this->assign('tasks', $tasks);
        $this->assign('page', $Page->show());
        //你月日设置
        $this->assign('years', $this->getYears($year));
        $this->assign('months', $this->getMonths($month));
        $this->assign('grade', $grade);
        $this->assign('grades', $grades);
        $this->assign('taskCount', $taskCount);
        $this->assign('taskImageCount', $taskImageCount);
        $this->display();
    }
}